home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / ToolManager / Source / Library / appmsgs.c next >
C/C++ Source or Header  |  1998-06-17  |  6KB  |  276 lines

  1. /*
  2.  * appmsgs.c  V3.1
  3.  *
  4.  * ToolManager WB application messages handling routines
  5.  *
  6.  * Copyright (C) 1990-98 Stefan Becker
  7.  *
  8.  * This source code is for educational purposes only. You may study it
  9.  * and copy ideas or algorithms from it for your own projects. It is
  10.  * not allowed to use any of the source codes (in full or in parts)
  11.  * in other programs. Especially it is not allowed to create variants
  12.  * of ToolManager or ToolManager-like programs from this source code.
  13.  *
  14.  */
  15.  
  16. #include "toolmanager.h"
  17.  
  18. /* Local data */
  19. static ULONG           WBLockCount     = 0;
  20. static struct Library *WorkbenchBase   = NULL;
  21. static struct MsgPort *AppMessagesPort;
  22.  
  23. /* Activate WB application messages */
  24. #define DEBUGFUNCTION StartAppMessages
  25. LONG StartAppMessages(void)
  26. {
  27.  LONG rc = -1;
  28.  
  29.  APPMSGS_LOG(LOG0(Entry))
  30.  
  31.  /* Allocate message port */
  32.  if (AppMessagesPort = CreateMsgPort()) rc = AppMessagesPort->mp_SigBit;
  33.  
  34.  APPMSGS_LOG(LOG2(Result, "Port 0x%08lx Signal %ld", AppMessagesPort, rc))
  35.  
  36.  return(rc);
  37. }
  38.  
  39. /* Stop WB application messages */
  40. #undef  DEBUGFUNCTION
  41. #define DEBUGFUNCTION StopAppMessages
  42. void StopAppMessages(void)
  43. {
  44.  APPMSGS_LOG(LOG1(Port, "0x%08lx", AppMessagesPort))
  45.  
  46.  SafeDeleteMsgPort(AppMessagesPort);
  47. }
  48.  
  49. /* Handle WB application messages */
  50. #undef  DEBUGFUNCTION
  51. #define DEBUGFUNCTION HandleAppMessages
  52. void HandleAppMessages(void)
  53. {
  54.  struct AppMessage *msg;
  55.  
  56.  APPMSGS_LOG(LOG0(Entry))
  57.  
  58.  /* Empty message queue */
  59.  while (msg = (struct AppMessage *) GetMsg(AppMessagesPort)) {
  60.  
  61.   APPMSGS_LOG(LOG2(Activating, "Object 0x%08lx Args 0x%08lx", msg->am_ID, msg))
  62.  
  63.   /* Activate object */
  64.   DoMethod((Object *) msg->am_ID, TMM_Activate, msg);
  65.  
  66.   /* Reply message */
  67.   ReplyMsg((struct Message *) msg);
  68.  }
  69. }
  70.  
  71. /* Open workbench.library */
  72. static BOOL LockWorkbench(void)
  73. {
  74.  BOOL rc;
  75.  
  76.  /* Workbench already opened or can we open it? */
  77.  if (rc = (WorkbenchBase != NULL) ||
  78.           (WorkbenchBase = OpenLibrary("workbench.library", 39)))
  79.  
  80.   /* Increment lock counter */
  81.   WBLockCount++;
  82.  
  83.  return(rc);
  84. }
  85.  
  86. /* Close workbench.library */
  87. static void ReleaseWorkbench(void)
  88. {
  89.  /* Decrement lock counter */
  90.  if (--WBLockCount == 0) {
  91.  
  92.   /* Lock count is zero, close library */
  93.   CloseLibrary(WorkbenchBase);
  94.  
  95.   /* Reset library base pointer */
  96.   WorkbenchBase = NULL;
  97.  }
  98. }
  99.  
  100. /* Remove all associated messages from the message port */
  101. #undef  DEBUGFUNCTION
  102. #define DEBUGFUNCTION RemoveAppMessages
  103. static void RemoveAppMessages(Object *obj)
  104. {
  105.  struct AppMessage *msg;
  106.  
  107.  APPMSGS_LOG(LOG1(Object, "0x%08lx", obj))
  108.  
  109.  /* Disable multi-tasking */
  110.  Forbid();
  111.  
  112.  /* Scan message port list */
  113.  msg = (struct AppMessage *) GetHead((struct MinList *) &AppMessagesPort
  114.                                        ->mp_MsgList);
  115.  while (msg) {
  116.   struct AppMessage *nextmsg = (struct AppMessage *)
  117.                                 GetSucc((struct MinNode *) msg);
  118.  
  119.   /* Does the message point to this object? */
  120.   if ((Object *) msg->am_ID == obj) {
  121.  
  122.    /* Remove it from list */
  123.    Remove((struct Node *) msg);
  124.  
  125.    /* Reply it */
  126.    ReplyMsg((struct Message *) msg);
  127.   }
  128.  
  129.   /* Next message */
  130.   msg = nextmsg;
  131.  }
  132.  
  133.  /* Enable multi-tasking */
  134.  Permit();
  135.  
  136.  /* Release Workbench */
  137.  ReleaseWorkbench();
  138. }
  139.  
  140. /* Create WB menu item */
  141. #undef  DEBUGFUNCTION
  142. #define DEBUGFUNCTION CreateAppMenuItem
  143. void *CreateAppMenuItem(Object *obj)
  144. {
  145.  void *rc = NULL;
  146.  
  147.  APPMSGS_LOG(LOG1(Arguments, "Object 0x%08lx", obj))
  148.  
  149.  /* Lock Workbench */
  150.  if (LockWorkbench()) {
  151.   char *name;
  152.  
  153.   APPMSGS_LOG(LOG0(WB locked))
  154.  
  155.   /* Get name from object */
  156.   GetAttr(TMA_ObjectName, obj, (ULONG *) &name);
  157.  
  158.   APPMSGS_LOG(LOG2(Name, "'%s' (0x%08lx)", name, name))
  159.  
  160.   /* Create menu item */
  161.   if ((rc = AddAppMenuItemA((ULONG) obj, 0, name, AppMessagesPort, NULL))
  162.        == NULL)
  163.  
  164.    /* Error, release workbench */
  165.    ReleaseWorkbench();
  166.  }
  167.  
  168.  APPMSGS_LOG(LOG1(Result, "0x%08lx", rc))
  169.  
  170.  return(rc);
  171. }
  172.  
  173. /* Delete WB menu item */
  174. #undef  DEBUGFUNCTION
  175. #define DEBUGFUNCTION DeleteAppMenuItem
  176. void DeleteAppMenuItem(void *appobj, Object *obj)
  177. {
  178.  APPMSGS_LOG(LOG2(Arguments, "AppObj 0x%08lx Object 0x%08lx", appobj, obj))
  179.  
  180.  /* Remove menu item */
  181.  RemoveAppMenuItem(appobj);
  182.  
  183.  /* Remove messages and release Workbench */
  184.  RemoveAppMessages(obj);
  185. }
  186.  
  187. /* Create WB icon */
  188. #undef  DEBUGFUNCTION
  189. #define DEBUGFUNCTION CreateAppIcon
  190. void *CreateAppIcon(Object *obj, struct DiskObject *dobj, BOOL showname)
  191. {
  192.  void *rc = NULL;
  193.  
  194.  APPMSGS_LOG(LOG2(Arguments, "Object 0x%08lx DiskObject 0x%08lx", obj, dobj))
  195.  
  196.  /* Lock Workbench */
  197.  if (LockWorkbench()) {
  198.   char *name = "";
  199.  
  200.   APPMSGS_LOG(LOG0(WB locked))
  201.  
  202.   /* Show name? */
  203.   if (showname)
  204.  
  205.    /* Yes, get name from object */
  206.    GetAttr(TMA_ObjectName, obj, (ULONG *) &name);
  207.  
  208.   APPMSGS_LOG(LOG2(Name, "'%s' (0x%08lx)", name, name))
  209.  
  210.   /* Create icon */
  211.   if ((rc = AddAppIconA((ULONG) obj, 0, name, AppMessagesPort, NULL, dobj,
  212.                         NULL)) == NULL)
  213.  
  214.    /* Error, release workbench */
  215.    ReleaseWorkbench();
  216.  }
  217.  
  218.  APPMSGS_LOG(LOG1(Result, "0x%08lx", rc))
  219.  
  220.  return(rc);
  221. }
  222.  
  223. /* Delete WB icon */
  224. #undef  DEBUGFUNCTION
  225. #define DEBUGFUNCTION DeleteAppIcon
  226. void DeleteAppIcon(void *appobj, Object *obj)
  227. {
  228.  APPMSGS_LOG(LOG2(Arguments, "AppObj 0x%08lx Object 0x%08lx", appobj, obj))
  229.  
  230.  /* Remove icon */
  231.  RemoveAppIcon(appobj);
  232.  
  233.  /* Remove messages and release Workbench */
  234.  RemoveAppMessages(obj);
  235. }
  236.  
  237. /* Create WB application window */
  238. #undef  DEBUGFUNCTION
  239. #define DEBUGFUNCTION CreateAppWindow
  240. void *CreateAppWindow(Object *obj, struct Window *w)
  241. {
  242.  void *rc = NULL;
  243.  
  244.  APPMSGS_LOG(LOG2(Arguments, "Object 0x%08lx Window 0x%08lx", obj, w))
  245.  
  246.  /* Lock Workbench */
  247.  if (LockWorkbench()) {
  248.  
  249.   APPMSGS_LOG(LOG0(WB locked))
  250.  
  251.   /* Create window */
  252.   if ((rc = AddAppWindowA((ULONG) obj, 0, w, AppMessagesPort, NULL)) == NULL)
  253.  
  254.    /* Error, release workbench */
  255.    ReleaseWorkbench();
  256.  }
  257.  
  258.  APPMSGS_LOG(LOG1(Result, "0x%08lx", rc))
  259.  
  260.  return(rc);
  261. }
  262.  
  263. /* Delete WB application window */
  264. #undef  DEBUGFUNCTION
  265. #define DEBUGFUNCTION DeleteAppWindow
  266. void DeleteAppWindow(void *appobj, Object *obj)
  267. {
  268.  APPMSGS_LOG(LOG2(Arguments, "AppObj 0x%08lx Object 0x%08lx", appobj, obj))
  269.  
  270.  /* Remove window */
  271.  RemoveAppWindow(appobj);
  272.  
  273.  /* Remove messages and release Workbench */
  274.  RemoveAppMessages(obj);
  275. }
  276.